﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 22:14:56
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{

    /// <summary>
    ///身份证信息 数据访问层
    /// </summary>	
    internal partial class IDCard
    {

        /// <summary>
        /// 根据断言条件获取单个身份证信息
        /// </summary>
        public IDCardInfo QuerySingle(Expression<Func<IDCardInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据断言条件获获取身份证信息列表
        /// </summary>
        public IList<IDCardInfo> QueryList(Expression<Func<IDCardInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据身份证和用户查询
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="idNum">身份证号</param>
        /// <returns></returns>
        public IDCardInfo GetByUserIDAndNum(long userId, string idNum)
        {
            Model.IDCardInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [IDCard] ");
            strSql.Append("WHERE [UserID]=@UserID ");
            strSql.Append("AND [IDNum]=@IDNum ");
            strSql.Append("AND [IsActive]=1 ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserID", DbType.Int64, userId);
            db.AddInParameter(dbc, "@IDNum", DbType.String, idNum);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        /// <summary>
        /// 根据用户查询
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public IDCardInfo GetByUserID(long userId)
        {
            Model.IDCardInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [IDCard] ");
            strSql.Append("WHERE [UserID]=@UserID ");
            strSql.Append("AND [IsActive]=1 ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserID", DbType.Int32, userId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }
    }

}